﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebComponents.Data;
using WebComponents.Security;
using WebComponents;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;

public partial class BannerLeft : System.Web.UI.UserControl {
  protected void Page_Load(object sender, EventArgs e) {
    if (!Page.IsPostBack) {
      AdvertiseInfo ads = AdvertiseController.GetByAdsId(ConvertType.ToInt(Request.Params["id"]));
      if (ads != null) {
        txtWidth.Text = ads.Width.ToString();
        txtHeight.Text = ads.Height.ToString();
        txtTieuDe.Text = ads.AlternateText;
        txtURL.Text = ads.NavigateUrl;
        txtViTri.Text = ads.OrderId.ToString();
        cmbViTri.SelectedIndex = -1;
        if (cmbViTri.Items.FindByValue(ads.Position) != null)
          cmbViTri.Items.FindByValue(ads.Position).Selected = true;
        if (ads.Status == 1)
          chkActive.Checked = true;
        else
          chkActive.Checked = false;
        for (int i = 0; i < cmdTarget.Items.Count; i++) {
          if (cmdTarget.Items[i].Text == ads.Target)
            cmdTarget.SelectedIndex = i;
        }
        lblBanner.Text = Common.LoadBannerMedia(Page, ads.MediaUrl, ads.Width, 300, ads.Height, 200, ads.NavigateUrl, ads.Target, ads.AlternateText);
        lblTitle.Text = "Cập nhật thông tin quảng cáo";
        btnSave.Text = "Cập nhật quảng cáo";
      } else {
        lblTitle.Text = "Thêm thông tin quảng cáo";
        btnSave.Text = "Thêm quảng cáo";
        List<AdvertiseInfo> list = AdvertiseController.List();
        if (list.Count > 0)
          txtViTri.Text = ConvertType.ToString(list.Max(a => a.OrderId) + 1);
        else
          txtViTri.Text = "1";
      }
      LoadImg();
    }
  }
  private void LoadImg() {
    cmbImage.Items.Clear();
    System.IO.FileInfo[] fileInfo = new DirectoryInfo(Server.MapPath("../userupload/Banner")).GetFiles();
    foreach (System.IO.FileInfo file in fileInfo) {
      if (file.Extension != ".db") {
        cmbImage.Items.Add(new ListItem(file.Name, "UserUpload/Banner/" + file.Name));
      }
    }
  }

  protected void btnSave_Click1(object sender, EventArgs e) {
    int id = ConvertType.ToInt(Request.Params["id"]);
    AdvertiseInfo ads = null;
    if (id == 0)
      ads = new AdvertiseInfo();
    else
      ads = AdvertiseController.GetByAdsId(id);
    if (ads != null) {
      HttpPostedFile file = fileUploadMedia.PostedFile;
      string filename = "";
      if (file != null && file.ContentLength > 0 && !string.IsNullOrEmpty(file.FileName) && (WebComponents.Security.Security.IsImage(file) || WebComponents.Security.Security.IsFlash(file))) {
        if (!File.Exists(Server.MapPath("../UserUpload/Banner/") + fileUploadMedia.FileName)) {
          file.SaveAs(Server.MapPath("../UserUpload/Banner/") + fileUploadMedia.FileName);
          filename = "UserUpload/Banner/" + fileUploadMedia.FileName;
        } else {
          if (chkGhiDe.Checked) {
            file.SaveAs(Server.MapPath("../UserUpload/Banner/") + fileUploadMedia.FileName);
            filename = "UserUpload/Banner/" + fileUploadMedia.FileName;
          } else {
            lblMessage.Text = "Tên tập tin đã tồn tại";
            return;
          }
        }
        LoadImg();
      }
      if (filename != "") {
        ads.MediaUrl = filename;
      } else if (chkSelect.Checked) {
        ads.MediaUrl = cmbImage.SelectedValue;
      }
      ads.Width = ConvertType.ToInt(txtWidth.Text);
      ads.Height = ConvertType.ToInt(txtHeight.Text);
      ads.OrderId = ConvertType.ToInt(txtViTri.Text);
      ads.Target = cmdTarget.SelectedValue;
      ads.Position = cmbViTri.SelectedValue;
      ads.NavigateUrl = txtURL.Text;
      ads.AlternateText = txtTieuDe.Text;
      if (chkActive.Checked)
        ads.Status = 1;
      else
        ads.Status = 0;
      if (id > 0)
        AdvertiseController.Update(ads);
      else
        AdvertiseController.Insert(ads);
      lblBanner.Text = Common.LoadBannerMedia(Page, ads.MediaUrl, ads.Width, 300, ads.Height, 200, ads.NavigateUrl, ads.Target, ads.AlternateText);
      Security.ShowClientMessage(Page, "Cập nhật thông tin thành công!", "?tabid=9");
    }
  }
  protected void chkSelect_CheckedChanged(object sender, EventArgs e) {
    if (chkSelect.Checked) {
      cmbImage.Visible = true;
      fileUploadMedia.Enabled = false;
      chkGhiDe.Enabled = false;
    } else {
      cmbImage.Visible = false;
      fileUploadMedia.Enabled = true;
      chkGhiDe.Enabled = true;
    }
  }
  protected void cmbImage_SelectedIndexChanged(object sender, EventArgs e) {
    lblBanner.Text = Common.LoadBannerMedia(Page, cmbImage.SelectedValue, Convert.ToInt16(txtWidth.Text), 300, Convert.ToInt16(txtHeight.Text), 200, "", "", "");
  }
  protected void cmbLanguage_SelectedIndexChanged(object sender, EventArgs e) {
    AdvertiseInfo ads = AdvertiseController.GetByAdsId(ConvertType.ToInt(Request.Params["id"]));
    if (ads != null) {
      txtWidth.Text = ads.Width.ToString();
      txtHeight.Text = ads.Height.ToString();
      txtTieuDe.Text = ads.AlternateText;
      txtURL.Text = ads.NavigateUrl;
      txtViTri.Text = ads.OrderId.ToString();
      if (ads.Status == 1)
        chkActive.Checked = true;
      else
        chkActive.Checked = false;
      for (int i = 0; i < cmdTarget.Items.Count; i++) {
        if (cmdTarget.Items[i].Text == ads.Target)
          cmdTarget.SelectedIndex = i;
      }
      lblBanner.Text = Common.LoadBannerMedia(Page, ads.MediaUrl, ads.Width, 300, ads.Height, 200, ads.NavigateUrl, ads.Target, ads.AlternateText);
    }
  }
}
